//
//  OTAViewController.m
//  avocado
//
//  Created by 彭思远 on 2022/3/26.
//  Copyright © 2022 Peng. All rights reserved.
//

#import "OTAViewController.h"

@interface OTAViewController ()<PPBleStateInterface,PPDataInterface, PPOtaInterface>
@property (weak, nonatomic) IBOutlet UILabel *progressLabel;
@property (weak, nonatomic) IBOutlet UIProgressView *progressView;

@property (nonatomic, strong) PPScaleManager *scaleManager;
@property (nonatomic, strong) UserModel *currentUser;
@property (nonatomic, strong) UILabel *l;
@end

@implementation OTAViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.l = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 100, 20)];
    [self.view addSubview:self.l];
    self.l.text = @"aaaaaaaaaaa";
    self.progressLabel.text = @"xxxxxxxxxxx";
    self.progressView.progress = 0.8;

}

- (void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setupPPScaleManager];
    [self.scaleManager startSearching];
}



- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.scaleManager disconnect];
}


-(void)setupPPScaleManager{
    
    
//    User *user = [IdentityManager manager].currentUser;
//    PPUserModel *userModel = [[PPUserModel alloc] initWithUserHeight:user.heightCm gender:user.sex age:user.age pregnantMode:NO athleteMode:NO  andUnit:user.unitType];
    self.currentUser = [IdentityManager manager].currentUser;

    PPUserModel *userModel = [[PPUserModel alloc] initWithUserHeight:self.currentUser.userHeight gender:self.currentUser.userGender age:self.currentUser.userAge pregnantMode:self.currentUser.isPregnantMode athleteMode:self.currentUser.isAthleteMode andUnit:self.currentUser.userUnit];
    self.scaleManager = [[PPScaleManager alloc] initWithMacAddressList:@[self.deviceMac] andUserModel:userModel];
    
    self.scaleManager.stateInterface = self;
    self.scaleManager.dataInterface = self;
    self.scaleManager.otaInterface = self;
    
}

- (void)monitorBluetoothWorkState:(PPBleWorkState)state{
    if (state == PPBleWorkStateDisconnected) {
        [self.scaleManager reStartSearching];
        self.scaleManager.stateInterface = nil;
    }
    
    if (state == PPBleWorkStateConnected) {
            
        [self.scaleManager enterInsideModel];
    }
    
    if (state == PPBleWorkStateCouldStartOTA) {
        NSString *otaPath = [[NSBundle mainBundle] pathForResource:@"cf818_ota_20_v3.1" ofType:@"bin"];
        NSData *otaData = [NSData dataWithContentsOfFile:otaPath];
        [self.scaleManager ota:otaData];
    }
}


- (void)scaleManager:(PPScaleManager *)manager monitorSerialNumberString:(NSString *)sn{
    if ([sn isEqualToString:@"20220316"]) {
        [manager enterSleepModel];
        [ProgressHud showText:@"升级成功"];
        [self.navigationController popViewControllerAnimated:YES];
    }
}


- (void)readyForStartOTA:(PPScaleManager *)manager{
    
    [manager otaUpdate];
    
}

- (void)scaleManager:(PPScaleManager *)manager monitorOTAProgress:(CGFloat)progress{
    
    dispatch_sync(dispatch_get_main_queue(), ^{
        NSString *s = [NSString stringWithFormat:@"同步进度:%.0f%%", progress * 100];
        self.progressLabel.text = s;
        self.progressView.progress = progress;
        self.l.text = s;
    });

}


@end
